package com.hmdp.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectionException;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        try{
            metaObject.setValue("updateTime", now);
        }catch (ReflectionException e){
            log.info("updateTime字段不存在");
        }
        try{
            metaObject.setValue("createTime", now);
        }catch (ReflectionException e){
            log.info("updateTime字段不存在");
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        try{
            metaObject.setValue("updateTime", now);
        }catch (ReflectionException e){
            log.info("updateTime字段不存在");
        }
    }
}
